
package jason.asSyntax;

import jason.asSyntax.parser.Token;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Represents the import directive. <br />
 * <code>{import name="path"}</code>
 */
public class Importive extends Atom {
	
	private final Token name;
	private final String path;
	
	public Importive(Token name, StringTerm path) {
		super("import");
		this.name = name;
		this.path = (path == null ? null : path.getString());
	}
	
	public Importive(Importive importive) {
		super(importive);
		this.name = importive.name;
		this.path = importive.path;
	}
	
	@Override
	protected int calcHashCode() {
		int result = super.calcHashCode();
		result = 7 * result + name.hashCode();
		result = 11 * result + path.hashCode();
		return result;
	}
	
	@Override
	public Importive clone() {
		return new Importive(this);
	}
	
	@Override
	public Element getAsDOM(Document document) {
		Element u = (Element)document.createElement("structure");
		u.setAttribute("functor", getFunctor());
		u.setAttribute("name", getName());
		u.setAttribute("path", getPath());
		return u;
	}
	
	public String getName() {
		return name.image;
	}
	
	public Token getToken() {
		return name;
	}
	
	public String getPath() {
		return path;
	}
	
	@Override
	public String toString() {
		if (path == null) return "{import " + name + "}";
		return "{import " + name + "=\"" + path + "\"}";
	}
}
